Page({
  data: {
    filteredLists: [],
    ele: '',
    array: ['>10km', '10<20km', '>20km'],
    array1: ['美食', '住宿', '游玩'],
    array2: ['>50', '50<200', '>200'],
  },
  callVenueListSelection: function (e) {
    const { key } = e.currentTarget.dataset;
    const value = e.detail.value;
    let distance = 0;
    let type = '';
    let money = 0;
    switch (key) {
      case 'distance':
        if (value == 0) {
          distance = 1;
        } else if (value == 1) {
          distance = 11;
        } else if (value == 2) {
          distance = 21;
        }
        break;
      case 'type':
        type = this.data.array1[value];
        break;
      case 'money':
        if (value == 0) {
          money = 0;
        } else if (value == 1) {
          money = 51;
        } else if (value == 2) {
          money = 201;
        }
        break;
      default:
        break;
    }
    console.log(distance);
    this.getDataFromServer(`http://localhost:8080/venue/listSelection?distance=${distance}&type=${type}&money=${money}`);
  },
  onLoad: function() {
    this.getDataFromServer('http://localhost:8080/venue/selectAll');
  },
  inputChange: function (e) {
    this.setData({
      ele: e.detail.value
    });
  },
  searchData: function () {
    console.log(this.data.ele + "输入值");
    const ele = this.data.ele;
    this.getDataFromServer('http://localhost:8080/venue/search', { ele: ele });
  },
  getDataFromServer: function(url, requestData = {}) {
    wx.request({
      url: url,
      data: requestData,
      success: res => {
        const data = res.data;
        if (data.code === 200) {
          const filteredLists = data.data.map(item => {
            const matches = item.match(/VenueShop\(id=(\d+), name=(.*?), address=(.*?), path=(.*?), distance=(.*?), type=(.*?), money=(\d+)\)(.*)/);
            if (matches) {
              const venueId = matches[1];
              const venueName = matches[2];
              const venueAddress = matches[3];
              const venuePath = matches[4];
              const venueDistance = matches[5];
              const venueType = matches[6];
              const venueMoney = matches[7];
              const description = matches[8].replace(',', '').trim();
              return {
                imgSrc: venuePath,
                headline: venueName,
                modelTexts: [`${venueAddress}`, `距离: ${venueDistance}km`],
                options: description.split(',')
              };
            } else {
              return null;
            }
          }).filter(item => item !== null);
          this.setData({
            filteredLists: filteredLists,
            lists: filteredLists
          });
        } else {
          console.log('数据获取失败');
        }
      },
      fail: error => {
        console.error('发生错误:', error);
      }
    });
  }
});